package com.juyu.miliao.api.service;

import java.util.List;

import javax.annotation.Resource;

import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.handler.IntegralRewardsAndPunishmentsHandler;
import com.juyu.miliao.api.util.SystemMessageUtil;
import org.springframework.stereotype.Service;

import com.juyu.miliao.api.cache.ReportCache;
import com.juyu.miliao.api.dao.mysql.UserReportDao;
import com.juyu.miliao.api.domain.Report;
import com.juyu.miliao.api.domain.UserReport;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ReportService {

	@Resource
	private UserReportDao userReportDao;
	@Resource
	private SystemMessageUtil systemMessageUtil;
	@Resource
	private UserInfoDao userInfoDao;
	@Resource
	private IntegralRewardsAndPunishmentsHandler integralRewardsAndPunishmentsHandler;

	public List<Report> getReportAll() {
		return ReportCache.INSTANCE.getReportList();
	}

//	@Transactional(rollbackFor = Exception.class)
	public void insertReportUser(UserReport entity) {
		User reportUser = userInfoDao.getUserById(entity.getReportUserId());
		entity.setReportUserIsHost(reportUser.getIsHost());
		//添加被举报人是否为主播字段
		User sufferUser = userInfoDao.getUserById(entity.getSufferUserId());
		entity.setSufferUserIsHost(sufferUser.getIsHost());
		if(entity.getSufferUserIsHost()==1){
			 User user = userInfoDao.selectAdminByUnionId(sufferUser.getUserId());
			 entity.setAdminId(user.getAdminId());
			 entity.setAdminName(user.getOperationName());
		}else {
            entity.setAdminId(0);
            entity.setAdminName("");
        }
		int i = userReportDao.insertReportUser(entity);
		integralRewardsAndPunishmentsHandler.reported(entity.getSufferUserId());
		if(i>0){
			systemMessageUtil.userReport(entity.getReportUserId(),sufferUser.getNickName());

		}
	}

}
